package cn.leedsoft.and.crimeintent.controller.photo_gallery;

import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import cn.leedsoft.and.crimeintent.R;

/**
 * Created by leeds on 2016/8/31 14:07.
 */
public class PollService extends IntentService {
    private static final String TAG = "PollService";
    private static final long INTERVAL_ALARM = 60*1000;//1 min
    public static final String POLL_ALARM_STATE = "poll.alarm.state";
    public static final String ACTION_SHOW_NOTIFICATION = PollService.class.getName() + ".SHOW_NOTIFICATION";
    public static final String CUSTOM_PRIVATE = "cn.leedsoft.and.crimeintent.controller.photo_gallery.CUSTOM_PRIVATE";//自定义 私有权限
    public static final int BROADCAST_REQUEST_CODE = 0;
    public static final String BROADCAST_REQUEST_CODE_KEY = "BROADCAST_REQUEST_CODE_KEY";
    public static final String BROADCAST_NOTIFICATION = "BROADCAST_NOTIFICATION";

    public PollService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, ">>>>>>>>>>>received an intent :" + intent);
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if(connMgr.getActiveNetworkInfo() == null) {
            Toast.makeText(this, "网络不可用哦", Toast.LENGTH_SHORT).show();
            Log.d(TAG, ">>>>>>>>>>>>不可用啊不可用");
        }

        showBackgroundNotification();
    }

    private void showBackgroundNotification() {
        Resources resources = getResources();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, PhotoGalleryActivity.class), 0);

        Notification notification = new NotificationCompat.Builder(this)
                .setTicker(resources.getString(R.string.photo_noti_ticker))
                .setSmallIcon(android.R.drawable.ic_menu_report_image)
                .setContentTitle(resources.getString(R.string.photo_noti_title))
                .setContentText(resources.getString(R.string.photo_noti_text))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .build();
        Intent i = new Intent(ACTION_SHOW_NOTIFICATION);
        i.putExtra(BROADCAST_REQUEST_CODE_KEY, BROADCAST_REQUEST_CODE);
        i.putExtra(BROADCAST_NOTIFICATION, notification);
        sendOrderedBroadcast(i, CUSTOM_PRIVATE,null, null, Activity.RESULT_OK, null, null);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, ">>>>>>>>>服务已创建");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>>>>>>>>服务已停止");
    }

    public static void setAlarmService(Context context, boolean toOpen) {
        Intent intent = new Intent(context, PollService.class);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if(toOpen) {
            am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), INTERVAL_ALARM, pendingIntent);//android5.1后，低于60s的定时任务，被推荐使用Handler的delay message。而此处间隔，低于60s，被强制设置为60s
        } else {
            am.cancel(pendingIntent);
            pendingIntent.cancel();
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putBoolean(POLL_ALARM_STATE, toOpen).commit();
    }

    public static boolean isAlarmOn(Context context) {
        Intent intent = new Intent(context, PollService.class);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

        return pendingIntent != null;
    }
}
